package com.seraph.splitviewdemo;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.WindowManager;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class SplitViewDemo extends Activity  implements OnClickListener, OnLongClickListener {
    private static final int CAMERA_PIC_REQUEST = 1212;
	/** Called when the activity is first created. */
	
	private ImageView v1,v2,v3,v4;
	private LinearLayout l1, l2;
	private ScaleAnimation disappear, reappear, zoomIn, zoomOut;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        v1 = (ImageView)findViewById(R.id.view1);
        v1.setOnClickListener(this);
        v1.setOnLongClickListener(this);
        
        v2 = (ImageView)findViewById(R.id.view2);
        v2.setOnClickListener(this);
        v2.setOnLongClickListener(this);
        
        v3 = (ImageView)findViewById(R.id.view3);
        v3.setOnClickListener(this);
        v3.setOnLongClickListener(this);
        
        v4 = (ImageView)findViewById(R.id.view4);
        v4.setOnClickListener(this);
        v4.setOnLongClickListener(this);
        
        l1 = (LinearLayout)findViewById(R.id.linearLayout1);
        l2 = (LinearLayout)findViewById(R.id.linearLayout2);
        
        float one = (float)1.;
        float zero = (float)0.;
        float four = (float)4.;
        
        disappear = new ScaleAnimation(one, zero, one, zero);
        reappear = new ScaleAnimation(zero, one, zero, one);
        zoomIn = new ScaleAnimation(one, four, one, four);
        zoomOut = new ScaleAnimation(four, one, four, one);
        
    }

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
			case R.id.view1:
				//v1.startAnimation(zoomIn);
				l2.setVisibility(View.GONE);
				v2.setVisibility(View.GONE);
				break;
			case R.id.view2:
				l2.setVisibility(View.GONE);
				v1.setVisibility(View.GONE);
				break;
			case R.id.view3:
				l1.setVisibility(View.GONE);
				v4.setVisibility(View.GONE);
				break;
			case R.id.view4:
				l1.setVisibility(View.GONE);
				v3.setVisibility(View.GONE);
		}
			
		
	}

	@Override
	public boolean onLongClick(View v) {
		// TODO Auto-generated method stub
		
		v2.setVisibility(View.VISIBLE);
		v3.setVisibility(View.VISIBLE);
		v4.setVisibility(View.VISIBLE);
		v1.setVisibility(View.VISIBLE);
		l1.setVisibility(View.VISIBLE);
		l2.setVisibility(View.VISIBLE);
		
		return true;
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu){
        super.onCreateOptionsMenu(menu);
        int groupId = 0;
        int menuItemOrder = Menu.NONE;
        // Create menu ids for the event handler to reference
        int menuItemId1 = Menu.FIRST;
        // Create menu text
        int menuItemText1 = R.string.captureButtonText;
        // Add the items to the menu
        MenuItem menuItem1 = menu.add(groupId, menuItemId1, menuItemOrder, menuItemText1);
                //.setIcon(R.drawable.ic_menu_close_clear_cancel);
        
        return true;
	}
	
	// Handle events from the popup menu above   
    public boolean onOptionsItemSelected(MenuItem item){
        super.onOptionsItemSelected(item);
        
        switch(item.getItemId()){
            
        	//Capture Button
        	case (Menu.FIRST):
            	//Fire up a camera intent
        		Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
				startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
                return true;
                
        }
        return false;
    }
    
    //The result of the camera intent is handled here
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	// TODO Auto-generated method stub
    	super.onActivityResult(requestCode, resultCode, data);

    	  if (requestCode == CAMERA_PIC_REQUEST) {
    		  Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
    		  v1.setImageBitmap(thumbnail);
    		  sendEmail(thumbnail);
          }
    }
    
    private void sendEmail(Bitmap image)
    {
//    	Mail m = new Mail("shareview972@gmail.com", "somepassword");
//
//        String[] toArr = {"shareview972@gmail.com"};
//        m.setTo(toArr);
//        m.setFrom("shareview972@gmail.com");
//        m.setSubject("This is an email sent using my Mail JavaMail wrapper from an Android device.");
//        m.setBody("Email body.");
//
//        try {
//          m.addAttachment(image);
//
//          if(m.send()) {
//            Toast.makeText(this, "Email was sent successfully.", Toast.LENGTH_LONG).show();
//          } else {
//            Toast.makeText(this, "Email was not sent.", Toast.LENGTH_LONG).show();
//          }
//        } catch(Exception e) {
//          //Toast.makeText(MailApp.this, "There was a problem sending the email.", Toast.LENGTH_LONG).show();
//          Log.e("MailApp", "Could not send email", e);
//        }
    	
    	Intent email = new Intent(Intent.ACTION_SEND);     
    	email.putExtra(Intent.EXTRA_EMAIL, new String[]{"shareview972@gmail.com"});     
    	email.putExtra(Intent.EXTRA_TEXT, "First Email from Android");     
    	email.putExtra(Intent.EXTRA_SUBJECT, "Subject & Predicate");
    	email.setType("message/rfc822");  
    	startActivity(email);  
    }
    
    
}